package com.sx.gongzhong.net;


import android.content.Intent;

import com.sx.gongzhong.MyApplication;
import com.sx.gongzhong.activity.LoginActivity;
import com.sx.gongzhong.model.ReplyUserBean;

import cn.jpush.android.api.JPushInterface;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by ZYG on 2017/11/17.
 * 回调的基类
 */

public abstract class BaseCallBack<T extends ReplyUserBean.BaseResult> implements Callback<T> {

    @Override
    public void onResponse(Call<T> call, Response<T> response) {
        if (response.raw().code() != 200) {
            onFailures("服务器异常！请稍后再试");
            return;
        }
        if (response.body() != null){
            if (response.body().code == 20000){
                onFailures(response.body().getMsg());
                Intent intent = new Intent(MyApplication.context, LoginActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                JPushInterface.stopPush(MyApplication.context);
//                MyApplication.context.finishAllActivity();
                MyApplication.context.startActivity(intent);
                return;
            }else if (response.body().code != 0){
                onFailures(response.body().msg);
                return;
            }
            onSuccess(response.body());
        }else {
            onFailures("返回数据体为空");
        }
    }

    @Override
    public void onFailure(Call<T> call, Throwable t) {//网络问题会走该回调
        onFailures(t.getMessage());
    }

    public abstract void onSuccess(T response);

    public abstract void onFailures(String message);
}
